package com.ayuku.commons.business.appmenu;

import java.util.List;

import com.ayuku.commons.AyukuDB;
import com.ayuku.retail.persistence.model.AppFunctions;
import com.ayuku.retail.persistence.model.AppUsers;

public class AppMenuImpl {

	public static List<AppFunctions> getMenuLevel(String arg) throws Exception {
		List<AppFunctions> lst = AyukuDB
				.getPersist()
				.readList(
						AppFunctions.class,
						"SELECT * FROM app_functions where depth = ? order by id, display_order",
						arg);
		return lst;
	}

	public static List<AppFunctions> getMenuChilds(String arg) throws Exception {
		List<AppFunctions> lst = AyukuDB.getPersist().readList(
				AppFunctions.class,
				"SELECT * FROM app_functions where parent_id = ?", arg);
		return lst;
	}

	public static List<AppFunctions> getAllMenu(String userId) throws Exception {
		List<AppFunctions> lst = null;
		AppUsers pj = (AppUsers) AyukuDB.getPersist().readByPrimaryKey(
				AppUsers.class, new Object[] { userId.trim() });
		if (pj != null) {
			String groupId = pj.getGroupId();
			System.out.println("GROUP iD: " + groupId);

			if (groupId == null) {
				System.out.println("GROUP iD null: " + groupId);
				groupId = "xx-x-xasqertfdfgdfgdf";
			}

			String sql = "SELECT * FROM app_functions "
					+ " where id in (select func_id from "
					+ " app_user_group_func where group_id = ? and "
					+ " (access_view = 'true' OR access_add='true' "
					+ " OR access_edit = 'true'  OR access_delete='true' )) order by depth, display_order, id asc";

			sql = "SELECT a.* FROM app_functions a "
					+ " INNER JOIN app_user_group_func b "
					+ " on a.id = b.func_id where b.group_id = ? "
					+ " and (b.access_view = 'true' OR b.access_add='true' OR b.access_edit = 'true'  OR b.access_delete='true' ) "
					+ " order by a.depth, a.display_order, a.id asc ";

			lst = AyukuDB.getPersist().readList(AppFunctions.class, sql,
					new Object[] { groupId });
		}
		return lst;
	}

	public static List<AppFunctions> getAllFunctions(String arg)
			throws Exception {
		List<AppFunctions> lst = AyukuDB.getPersist().readList(
				AppFunctions.class,
				"SELECT * FROM app_functions order by id, display_order");
		return lst;
	}

}
